  set more off
  mat drop _all
  set seed 19112014

*===================================================================*
*   BIHAR EVALUATION OF SOCIAL FRANCHISING AND TELEMEDICINE (BEST)
*                           ANCOVA ANALYSIS
*====================================================================*

*====================================================================*
* TABLE A8
*====================================================================*

*====================================================================*
* Diarrhea
*====================================================================*

* Merge Datasets First Round
  use "$prodata1\providers_interview_1st", clear

  merge 1:1 prov_id wave using "$prodata1\standardized_patient_diarrhea_b", keepus(case sample2 nhistory dx_type tx_any tx_detail1 tx_type sp_theta_mle_q12_std sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample2
  keep if sample1==1 & sample2==1
  drop sample2
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata1\vignette_diarrhea", keepus(vig_theta_mle_q12_std)
  keep if _merge==3
  drop _merge

  tempfile R1
  save "`R1'"


* Merge Datasets Second Round
  use "$prodata2\providers_interview_2nd", clear
  merge 1:1 prov_id wave using "$prodata2\standardized_patient_diarrhea_b", keepus(case sample2 nhistory dx_type tx_any tx_detail1 tx_type sp_theta_mle_q12_std sp_theta_mle_q12_stdb sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample2
  keep if sample1==1 & sample2==1
  drop sample2
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata2\vignette_diarrhea", keepus(vig_theta_mle_q12_stdb)
  keep if _merge==3
  drop _merge

  tempfile R2
  save "`R2'"

* Append Rounds
  use "`R1'", clear
  append using "`R2'"
  tab wave
  
* Sample selection 2
/*NOTE: we need to exclude WHP providers*/
  gen     regsample = 1
  replace regsample = 0 if WHP_oversampled==1

* Post variable
  gen post = (wave==2)

* Interaction 
  gen treat_post = treat*post

* Percentage of selected items asked
  egen pq_sp_d=rmean(sp_item1-sp_item11)

* Unnecessary and harmful treatments 
  gen tx_harm=(tx_detail1>=3 & tx_detail1<=5 | tx_detail1==7)
  table treat wave, c(mean  tx_harm)

* ORS
  gen     ORS = (tx_detail1<=5)
  lab var ORS "=1 if ORS given"

* Performance Score
  gen     perfscore = sp_theta_mle_q12_std if wave==1
  replace perfscore = sp_theta_mle_q12_stdb if wave==2

* Knowledge Score
  gen     knowscore = vig_theta_mle_q12_std if wave==1
  replace knowscore = vig_theta_mle_q12_stdb if wave==2


* Dependent variable
  local dep = "pq_sp_d perfscore ORS tx_harm"

* Outcome means at wave 1
  foreach var of local dep {
    bys cluster: egen aux`var'=mean(`var') if wave==1
    bys cluster: egen m`var'=min(aux`var')
    drop aux`var'
  }

* Regressors
  local reg1 "post treat"
  local reg2 "post treat age experience mqualif knowscore"
  local reg3 "post treat age experience mqualif knowscore work_hs npatient camps fac_gov niindex"

* Matrix to store results
  mat A8_sp = J(9,8,.)
  mat colnames A8_sp = N mean beta se ci_l ci_u pvalue adj_pvalue


* Regressions
  local i 1
  foreach var of local dep {
    forvalues r=1/3 {

    * Mean value of dependent variable 
      sum `var' if regsample==1
      local mean = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' m`var' `reg`r'' if regsample==1 & wave==2, absorb(district) cluster(cluster)

    * Fill matrix
      if `r'==3 {
      mat A8_sp[`i',1] = e(N)
      mat A8_sp[`i',2] = `mean'
      mat A8_sp[`i',3] = _b[treat]
      mat A8_sp[`i',4] = _se[treat]
      local low_ci  = _b[treat] - invttail(e(df_r),0.025)*_se[treat]
      local high_ci = _b[treat] + invttail(e(df_r),0.025)*_se[treat]
      mat A8_sp[`i',5] = `low_ci'
      mat A8_sp[`i',6] = `high_ci'
      local t = _b[treat]/_se[treat]
      mat A8_sp[`i',7] = 2*ttail(e(df_r),abs(`t'))
      }

    * Save results
      if "`dep'"=="pq_vig_d" & `r'==1 local app = "replace"
      else local app = "append"
      outreg2 using "$results\A8_diarrhea_sp.xls", keep(treat) ctitle(`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen addstat("Mean", `mean') addnote("District FE") `app'
    }
  local i = `i'+1
  }

* Erase txt files created by outreg
  erase "$results\A8_diarrhea_sp.txt"





*====================================================================*
* Pneumonia
*====================================================================*

* Merge Datasets First Round
  use "$prodata1\providers_interview_1st", clear

  merge 1:1 prov_id wave using "$prodata1\standardized_patient_pneumonia_b", keepus(case sample3 nhistory dx_type tx_any tx_detail3 tx_type sp_theta_mle_q12_std sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample3
  keep if sample1==1 & sample3==1
  drop sample3
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata1\vignette_pneumonia", keepus(vig_theta_mle_q12_std)
  keep if _merge==3
  drop _merge

  tempfile R1
  save "`R1'"


* Merge Datasets Second Round
  use "$prodata2\providers_interview_2nd", clear
  merge 1:1 prov_id wave using "$prodata2\standardized_patient_pneumonia_b", keepus(case sample3 nhistory dx_type tx_any tx_detail3 tx_type sp_theta_mle_q12_std sp_theta_mle_q12_stdb sp_item? sp_item??)
  keep if _merge==3
  drop _merge

  tab sample1 sample3
  keep if sample1==1 & sample3==1
  drop sample3
  count

/* Knowledge Score*/
  merge 1:1 prov_id wave using "$prodata2\vignette_pneumonia", keepus(vig_theta_mle_q12_stdb)
  keep if _merge==3
  drop _merge

  tempfile R2
  save "`R2'"

* Append Rounds
  use "`R1'", clear
  append using "`R2'"
  tab wave

* Sample selection 2
/*NOTE: we need to exclude WHP providers*/
  gen     regsample = 1
  replace regsample = 0 if WHP_oversampled==1

* Post variable
  gen post = (wave==2)

* Percentage of selected items asked
  egen pq_sp_p=rmean(sp_item1-sp_item9)

* Unnecessary and harmful treatments 
  gen tx_harm=(tx_detail3>=3 & tx_detail3<=5)

* Harmful or non treatment
  gen tx_harm_no=(tx_detail>=3 & tx_detail<=6)

* Antibiotics
  gen antibiotics=(tx_detail<=4)

* Performance Score
  gen     perfscore = sp_theta_mle_q12_std if wave==1
  replace perfscore = sp_theta_mle_q12_stdb if wave==2

* Knowledge Score
  gen     knowscore = vig_theta_mle_q12_std if wave==1
  replace knowscore = vig_theta_mle_q12_stdb if wave==2

* Dependent variable
  local dep = "pq_sp_p perfscore antibiotics tx_harm tx_harm_no"
  local dep = "pq_sp_p perfscore antibiotics tx_harm"

* Outcome means at wave 1
  foreach var of local dep {
    bys cluster: egen aux`var'=mean(`var') if wave==1
    bys cluster: egen m`var'=min(aux`var')
    drop aux`var'
  }

* Regressors
  local reg1 "post treat"
  local reg2 "post treat age experience mqualif knowscore"
  local reg3 "post treat age experience mqualif knowscore work_hs npatient camps fac_gov niindex"

* Regressions
  local i 6
  foreach var of local dep {
    forvalues r=1/3 {

    * Mean value of dependent variable 
      sum `var' if regsample==1
      local mean = string(round(r(mean)*100, .1))

    * Regressions
      areg `var' m`var' `reg`r'' if regsample==1 & wave==2, absorb(district) cluster(cluster)

    * Fill matrix
      if `r'==3 {
      mat A8_sp[`i',1] = e(N)
      mat A8_sp[`i',2] = `mean'
      mat A8_sp[`i',3] = _b[treat]
      mat A8_sp[`i',4] = _se[treat]
      local low_ci  = _b[treat] - invttail(e(df_r),0.025)*_se[treat]
      local high_ci = _b[treat] + invttail(e(df_r),0.025)*_se[treat]
      mat A8_sp[`i',5] = `low_ci'
      mat A8_sp[`i',6] = `high_ci'
      local t = _b[treat]/_se[treat]
      mat A8_sp[`i',7] = 2*ttail(e(df_r),abs(`t'))
      }

    * Save results
      if "`dep'"=="pq_sp_p" & `r'==1 local app = "replace"
      else local app = "append"
      outreg2 using "$results\A8_pneumonia_sp.xls", keep(treat) ctitle(`var') bdec(3) sdec(3) ///
      nocons stats(coef ci_low ci_high pval) noparen addnote("District FE") addstat("Mean", `mean') `app'
    }
    local i = `i'+1
  }

* Erase txt files created by outreg
  erase "$results\A8_pneumonia_sp.txt"

* Export tables
  preserve
  drop _all
  svmat2 A8_sp, names(col)
  foreach var of varlist beta-adj_pvalue {
    replace `var' = round(`var',.001)
  }
  outsheet using "$results\A8_sp.xls", replace
  restore
